package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.User;
import cn.hellochaos.generator.entity.dto.LogicFlagCode;
import cn.hellochaos.generator.entity.dto.ResultBean;
import cn.hellochaos.generator.entity.dto.ResultCode;
import cn.hellochaos.generator.mapper.UserMapper;
import cn.hellochaos.generator.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.HashMap;
import java.util.Map;

import static cn.hellochaos.generator.entity.dto.LogicFlagCode.DELETE_NOT_RESULT;

/**
* <p>
* 用户表 服务实现类
* </p>
*
* @author fmy
* @since 2021-07-17
*/
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page<User> listUsersByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询user: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<User> queryWrapper =  new QueryWrapper<User>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<User> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询user完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public User getUserById(int id) {
        log.info("正在查询user中id为{}的数据",id);
        User user = super.getById(id);
        log.info("查询id为{}的user{}",id,(null == user?"无结果":"成功"));
        return user;
    }

    @Override
    public User getUserByName(String name) {
        log.info("正在查询user中name为{}的数据",name);
        //MBP 在mapper自定义查询方法的话需要getbasemapper(),然后执行自定义方法
        super.getBaseMapper().findUserByName(name);

        return null;
    }

//    @Override
//    public int insertUser(User user) {
//        log.info("正在插入user");
//        if (super.save(user)) {
//            log.info("插入user成功,id为{}",user.getUserId());
//            return user.getUserId();
//        } else {
//            log.error("插入user失败");
//            throw new BizException("添加失败");
//        }
//    }

    @Override
    public int deleteUserById(int id) {
        log.info("正在删除id为{}的user",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的user成功",id);
            return id;
        } else {
            log.error("删除id为{}的user失败",id);
            return DELETE_NOT_RESULT;
            //throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateUser(User user) {
        log.info("正在更新id为{}的user",user.getUserId());
        if(super.getById(user.getUserId())==null){
            return LogicFlagCode.UPDATE_NOT_RESULT;
        }

        if (super.updateById(user)) {
            log.info("更新id为{}的user成功",user.getUserId());
            return user.getUserId();
        } else {
            log.error("更新id为{}的user失败",user.getUserId());
            throw new BizException("更新失败[id=" + user.getUserId() + "]");
        }
    }

    /**
     * 注册
     * @param user 参数封装
     * @return Result
     */
    @Override
    public ResultBean<Object> register(User user) {
        ResultBean<Object> resultBean; //= new ResultBean<>();
        Map map;
            User existUser = userMapper.findUserByName(user.getUsername());
            if(existUser != null){
                // 如果用户名已存在
                resultBean = new ResultBean<>(ResultCode.MSG_USER_HAS_EXIST,ResultCode.USER_HAS_EXIST);
                //return -1;
                //return resultBean;
                // result.setMsg("用户名已存在");
            }else{
                log.info("正在插入user");
                if (super.save(user)) {
                    log.info("插入user成功,id为{}",user.getUserId());
                    //log.info("插入user成功,id为{}",user.getUserId());
                    map=new HashMap(16);
                    map.put("user_id",user.getUserId());
                    resultBean = new ResultBean<>("注册成功",ResultCode.SUCCESS,map);
                    //return user.getUserId();
            }else{
                    log.error("插入user失败");
                    resultBean = new ResultBean<>("服务器内部错误","500",null);
                    //throw new BizException("添加失败");
                }
        }
        return resultBean;
    }
    /**
     * 登录
     * @param user 用户名和密码
     * @return Result
     */
    @Override
    public ResultBean<Object> login(User user) {
            ResultBean<Object> resultBean;
            Map map;
            Long userId= userMapper.login(user);
            if(userId == null){
                //用户名或密码错误,封装返回json
                resultBean = new ResultBean<>(ResultCode.MSG_USER_ACCOUNT_ERROR, ResultCode.USER_ACCOUNT_ERROR);
            }else{
                map=new HashMap(16);
                map.put("user_id",userId);

                log.info("登录成功");
                resultBean = new ResultBean<>(ResultCode.MSG_SUCCESS,ResultCode.SUCCESS,map);
            }

        return resultBean;
    }

}
